<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'invoice.add'"></el-button>
                <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'custom.delete'"></el-button> -->
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-input style="margin:5px 10px" v-model="search.customer_rise" placeholder="请输入客户抬头" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.company_name" placeholder="请输入开票主体" clearable @keyup.enter="upsearch"></el-input>
                    <!-- <el-select v-model="search.profession" placeholder="特殊标记" clearable>
                        <el-option label="全部" :value="0"/>
                        <el-option label="月结" :value="1"/>
                        <el-option label="现结" :value="2"/>
                    </el-select> -->
                    <el-date-picker v-model="search.time_range" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="客户编号" prop="customer.apply_code" align="center"></el-table-column>
                <el-table-column label="项目编号" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="客户抬头" prop="opendata.customer_rise" align="center"></el-table-column>
                <el-table-column label="抬头类型" prop="opendata.rise_type" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.opendata && scope.row.opendata.rise_type == 0">个人</div>
                        <div v-if="scope.row.opendata && scope.row.opendata.rise_type == 1">单位</div>
                    </template>
                </el-table-column>
                <el-table-column label="开票类型" prop="opendata.open_type" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.opendata && scope.row.opendata.open_type == 1">增值税纸质专票</div>
                        <div v-if="scope.row.opendata && scope.row.opendata.open_type == 2">增值税纸质普票</div>
                        <div v-if="scope.row.opendata && scope.row.opendata.open_type == 3">增值税电子普票</div>
                        <div v-if="scope.row.opendata && scope.row.opendata.open_type == 4">增值税电子专票</div>
                    </template>
                </el-table-column>
                <el-table-column label="纳税人识别号" prop="opendata.taxpayer_num" align="center"></el-table-column>
                <el-table-column label="开户行" prop="opendata.open_bank" align="center"></el-table-column>
                <el-table-column label="开户账号" prop="opendata.open_bank_account" align="center"></el-table-column>
                <el-table-column label="开票金额" prop="money" align="center"></el-table-column>
                <el-table-column label="开票主体" prop="company.company_name" align="center"></el-table-column>
                <el-table-column label="上传凭证" prop="withAdmin.name" align="center">
                    <template #default="scope">
                        <!-- <el-image class="img" :src="scope.row.goods_image" :preview-src-list="scope.row.goods_image_item" fit="cover" preview-teleported="true" hide-on-click-modal /> -->
                        <el-button type="text" @click="table_enclo(scope.row)" v-if="scope.row.voucher">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="审核状态" prop="status" align="center">
                    <template #default="scope">
                        <el-tag :type="scope.row.status==0?'warning':scope.row.status==1?'success':'danger'">{{scope.row.status==0?'待审核':scope.row.status==1?'审核通过':'审核拒绝'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_add(scope.row, scope.$index)" v-auth="'invoicecheck.upload'" v-if="scope.row.status==0">上传凭证</el-button>
                            <el-button text type="warning" size="small" @click="table_check(scope.row, scope.$index)" v-auth="'invoicecheck.check'" v-if="scope.row.status==0 && scope.row.voucher">审核</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <check-status v-if="dialog.status" ref="statusDialog" @success="handleSuccess" @closed="dialog.status = false"></check-status>
    <show-save v-if="dialog.show" ref="showDialog" @success="handleSuccess" @closed="dialog.show = false"></show-save>
    <el-dialog title="查看附件" v-model="encloVisible" width="500" align-center>
        <el-form :disabled="mode == 'show'" label-width="110px" label-position="right">
            <el-form-item label="附件" prop="voucher">
                <sc-upload-file v-model="show.voucher" :is_same_name="1">
                    <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                </sc-upload-file>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="encloVisible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
import ShowSave from '@/views/project/invoice/showSave.vue'
import CheckStatus from '../../submitted/checkStatus.vue'
import saveDialog from './save.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,CheckStatus,ShowSave
	},
	data() {
		return {
			dialog: {
				save: false,
				status: false,
				show: false,
			},
			apiObj: this.$API.project.invoice.list,
			selection: [],
			search: {
				customer_rise: null,
				company_name: null,
			},
            show: {
                id: "",
                voucher: "",
            },
            encloVisible: false,
            mode: 'add',
		}
	},
	mounted() {
	},
	methods: {
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        table_show(row) {
			this.dialog.show = true
			this.$nextTick(() => {
				this.$refs.showDialog.open('show').setData(row)
			})
		},
        // 审核
        table_check(row) {
			this.dialog.status = true
			this.$nextTick(() => {
				this.$refs.statusDialog.open('invoice').setData(row)
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'invvoice') {
				this.$refs.table.refresh()
			}
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        // 附件查看
        table_enclo(row){
            this.encloVisible = true
            this.show.voucher = row.voucher
            this.mode = 'show'
        },
        table_add(row){
            this.encloVisible = true
            this.show.id = row.id
            this.show.voucher = row.voucher
            this.mode = 'add'
        },
        async submit() {
            var res = await this.$API.project.invoice.upload.post(this.show);
            this.isSaveing = false;
            if (res.code == 1) {
                this.encloVisible = false;
                this.$message.success("操作成功")
				this.$refs.table.refresh()
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
	}
}
</script>

<style>
</style>
